#!/bin/bash
#------------------------------------------
#      Ubuntu12.04 asp.net InstallScript
#      copyright https://github.com/cjy37
#      email: rocky.cn@foxmail.com
#------------------------------------------

function showMenu()
{
	clear
	echo
	echo "--------------------------------------------------------------"
	echo "|      Ubuntu12.04 Install Helper                            |"
	echo "|      版权所有 https://github.com/cjy37                     |"
	echo "--------------------------------------------------------------"
	echo "|      a. 安装全部组件                                       |"
	echo "|      b. 安装 Web 服务 (linux mono mysql jexus)[c,d]        |"
	echo "|      c. 安装 Mono & Jexus (Asp.Net Web Server)             |"
	echo "|      d. 安装 Mysql (MariaDB-10)                            |"
	echo "|      e. 安装 Haproxy                                       |"
	echo "|      f. 安装 Redis                                         |"
	echo "|      g. 安装 Node.js                                       |"
	echo "|      h. 安装 MongoDB                                       |"
	echo "|      i. 安装 Nginx                                         |"
	echo "|      j. 安装 MQTT 服务                                     |"
	echo "|      k. *更新 Mono & Jexus                                 |"
	echo "|      l. *更新 Jexus (Mono Web Server)                      |"
	echo "|      x. 退出                                               |"
	echo "--------------------------------------------------------------"
	echo
	return 0
}

function selectCmd()
{
	alias cp='cp'
	showMenu
	echo "请选择要安装的字母序号 [a-x]:"
	read -n 1 M
	echo

	if [ "$M" = "x" ]; then
		exit 1
		
	elif [ "$M" = "a" ]; then
		echo "安装 全部组件..."
		echo "------------------------------------"
		setupNewMono && setupJws && setupMysql && setupHaproxy && setupRedis && setupNodeJS && setupMQTTServer && setupMongoDB && setupNginx
		read -n 1 -p "按 <Enter> 继续..."
		
	elif [ "$M" = "b" ]; then
		echo "安装 Web 服务(linux mono mysql jexus) (lmmj) [c,d]"
		echo "------------------------------------"
		setupNewMono && setupJws && setupMysql
		read -n 1 -p "按 <Enter> 继续..."
		
	elif [ "$M" = "c" ]; then
		setupNewMono && setupJws
		read -n 1 -p "按 <Enter> 继续..."

	elif [ "$M" = "d" ]; then
		setupMysql
		read -n 1 -p "按 <Enter> 继续..."
		
	elif [ "$M" = "e" ]; then
		setupHaproxy
		read -n 1 -p "按 <Enter> 继续..."
		
	elif [ "$M" = "f" ]; then
		setupRedis
		read -n 1 -p "按 <Enter> 继续..."
		
	elif [ "$M" = "g" ]; then
		setupNodeJS
		read -n 1 -p "按 <Enter> 继续..."
		
	elif [ "$M" = "j" ]; then
		setupMQTTServer
		read -n 1 -p "按 <Enter> 继续..."
		
	elif [ "$M" = "h" ]; then
		setupMongoDB
		read -n 1 -p "按 <Enter> 继续..."
		
	elif [ "$M" = "i" ]; then
		setupNginx
		read -n 1 -p "按 <Enter> 继续..."
		
	elif [ "$M" = "k" ]; then
		updateMono && updateJws
		read -n 1 -p "按 <Enter> 继续..."
		
	elif [ "$M" = "l" ]; then
		updateJws
		read -n 1 -p "按 <Enter> 继续..."

	else
		echo "选择错误!"
		read -n 1 -p "按 <Enter> 继续..."
	fi

	selectCmd
	return 0
}

function setupJava8()
{
	sudo apt-get install python-software-properties software-properties-common -y
	sudo add-apt-repository ppa:webupd8team/java
	sudo apt-get update
	echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | sudo /usr/bin/debconf-set-selections
	sudo apt-get install oracle-java8-installer oracle-java8-set-default -y
	javac -version
}

function setupLibs()
{
	sudo apt-get update -y
	sudo apt-get upgrade -y

	echo "安装公共组件..."
	echo "------------------------------------"

	if [ ! -d /wwwroot/htdocs/www ]; then
	  sudo mkdir /wwwroot
	  sudo chmod 777 wwwroot/
	  mkdir -pv /wwwroot/htdocs/www
	fi

	if [ ! -d /tmp/downloads ]; then
	  mkdir -pv /tmp/downloads
	fi

	cd /tmp
	sudo apt-get install -y vim chkconfig
	sudo apt-get install -y build-essential
	sudo apt-get install -y automake autoconf
	sudo apt-get install -y bison gettext libtool libglib2.0-dev libfreetype6-dev libfontconfig-dev
	sudo apt-get install -y libgif-dev libtiff4-dev libpng12-dev libexif-dev libx11-dev libxft-dev libjpeg-dev

	cd /tmp/downloads
	wget -c https://github.com/cjy37/linux-asp.net-installScript/raw/master/conf.zip
	if [ -f conf.zip ]; then
			unzip -o conf.zip
	fi

	return $?
}

function updateMono()
{
	echo "更新 mono"
	echo "------------------------------------"
	cd /tmp
	wget http://download.mono-project.com/repo/xamarin.gpg
	apt-key add xamarin.gpg
	#echo "deb http://download.mono-project.com/repo/debian wheezy main" > /etc/apt/sources.list.d/mono-xamarin.list
	echo "deb http://download.mono-project.com/repo/debian alpha main" | sudo tee /etc/apt/sources.list.d/mono-xamarin-alpha.list
	sudo apt-get update
	sudo apt-get install -y mono-complete
	sudo apt-get install -y ca-certificates-mono
	
	echo "更新 mono 完成. 信息:"
	mono -V
	echo "------------------------------------"
	return $?
}

function setupNewMono()
{
	if [ ! -d /wwwroot/htdocs/www ]; then
	  mkdir -pv /wwwroot/htdocs/www
	fi
	if [ ! -d /tmp/downloads ]; then
	  mkdir -pv /tmp/downloads
	fi
	
	echo "安装 mono"
	echo "------------------------------------"

	sudo apt-get -f remove -y libmono* mono-jay
	sudo apt-get autoremove -y
	sudo rm -rf /etc/apt/sources.list.d/mono-xamarin.list
	sudo apt-get update
	sudo apt-get install -y mono-complete

	cd /tmp
	wget http://download.mono-project.com/repo/xamarin.gpg
	sudo apt-key add xamarin.gpg
	# echo "deb http://download.mono-project.com/repo/debian wheezy main" > /etc/apt/sources.list.d/mono-xamarin.list
	echo "deb http://download.mono-project.com/repo/debian alpha main" | sudo tee /etc/apt/sources.list.d/mono-xamarin-alpha.list
	sudo apt-get update
	sudo apt-get install -y mono-complete
	sudo apt-get install -y ca-certificates-mono
	
	echo "安装 mono 完成. 信息:"
	mono -V
	echo "------------------------------------"
	return $?
}


function setupMysql()
{
	if [ ! -d /tmp/downloads ]; then
	  mkdir -pv /tmp/downloads
	fi

	echo "安装 mysql"
	echo "------------------------------------"

	sudo apt-get install -y python-software-properties
	sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
	sudo add-apt-repository 'deb http://ftp.osuosl.org/pub/mariadb/repo/10.0/ubuntu precise main'
	
	sudo apt-get update -y 
	sudo apt-get install -y mariadb-server mariadb-client libmariadbd*-dev
	sudo cp /usr/share/mysql/my-innodb-heavy-4G.cnf /etc/my.cnf
	#sudo sed -i 's/# generic configuration options/user = mysql/g' /etc/my.cnf
	sudo sed -i '/\[mysqld\]/a user = mysql' /etc/my.cnf
	sudo service mysql start
	mysql -V
	echo "------------------------------------"
	return $?
}

function setupHaproxy()
{
	echo "安装 haproxy"
	echo "------------------------------------"

	sudo apt-get install -y haproxy
	sudo echo "ENABLED=1" > /etc/default/haproxy
	sudo service haproxy start
	haproxy -v
	echo "------------------------------------"
	return $?
}

function setupRedis()
{
	echo "安装 Redis"
	echo "------------------------------------"

	sudo apt-get install -y redis-server
	sudo service redis-server start
	redis-server -v
	echo "------------------------------------"
	return $?
}

function setupMongoDB()
{
	echo "安装 MongoDB"
	echo "------------------------------------"

	sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
	echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
	sudo apt-get update -y
	sudo apt-get install -y mongodb-10gen
	sudo apt-get install -y mongodb-10gen=2.4.6
	echo "mongodb-10gen hold" | sudo dpkg --set-selections
	sudo service mongodb start
	mongod --version
	echo "------------------------------------"
	return $?
}

function setupMQTTServer()
{
	echo "安装 mosquitto(MQTT 服务)"
	echo "------------------------------------"

	sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
	sudo apt-get update -y
	sudo apt-get install -y python-software-properties
	sudo apt-get install -y mosquitto
	sudo cp /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf
	echo "Mosquitto 配置文件: /etc/mosquitto/mosquitto.conf"
	sudo service mosquitto start
	mosquitto -h
	echo "------------------------------------"
	return $?
}

function setupNodeJS()
{
	echo "安装 Node.js"
	echo "------------------------------------"
	
	if [ ! -d /wwwroot ]; then
	  sudo mkdir -pv /wwwroot
	fi
	
	if [ ! -d /wwwroot/node_mqtt ]; then
	  mkdir -pv /wwwroot/node_mqtt
	fi
	
	sudo apt-get install -y python-software-properties python g++ make build-essential git
	wget -qO- https://raw.github.com/creationix/nvm/master/install.sh | sh
	source ~/.bashrc
	nvm install 4.4.7
	nvm use 4.4.7
	nvm alias default 4.4.7

	node -v
	echo "------------------------------------"

	sudo echo "安装Node组件：node-gyp sails bower grunt-cli cordova ionic"
	sudo npm install -g node-gyp sails bower grunt-cli cordova ionic

	return $?
}

function setupNginx()
{
	echo "安装 Nginx"
	echo "------------------------------------"
	sudo apt-add-repository ppa:nginx/development
	sudo apt-get update
	sudo apt-get install nginx
	echo "Nginx 配置文件: /etc/nginx/nginx.conf"
	nginx -v
	echo "------------------------------------"
	return $?
}

function updateJws()
{
	if [ ! -d /wwwroot/htdocs/www ]; then
	  mkdir -pv /wwwroot/htdocs/www
	fi
	if [ ! -d /tmp/downloads ]; then
	  mkdir -pv /tmp/downloads
	fi

	echo "更新 jexus"
	echo "------------------------------------"

	cd /tmp/downloads
	wget -c http://www.linuxdot.net/down/jexus-5.8.1-x64.tar.gz
	if [ -f jexus-5.8.1-x64.tar.gz ]; then
	  tar -xf jexus-5.8.1-x64.tar.gz
	  cd jexus-5.8.1-x64
	  sudo /usr/jexus/jws stop
      sudo ./upgrade
	  sudo /usr/jexus/jws start
	  sudo /usr/jexus/jws -V
	  echo "------------------------------------"
	  echo "Jexus 更新到: /usr/jexus/"
	  echo "Jexus 配置文件： (/usr/jexus/jws.conf and /usr/jexus/siteconf/*)"
	fi
	
	return $?
}

function setupJws()
{
	if [ ! -d /wwwroot/htdocs/www ]; then
	  mkdir -pv /wwwroot/htdocs/www
	fi
	if [ ! -d /tmp/downloads ]; then
	  mkdir -pv /tmp/downloads
	fi

	echo "安装 jexus"
	echo "------------------------------------"

	cd /tmp/downloads
	wget -c http://www.linuxdot.net/down/jexus-5.8.1-x64.tar.gz
	if [ -f jexus-5.8.1-x64.tar.gz ]; then
	  tar -xf jexus-5.8.1-x64.tar.gz
	  cd jexus-5.8.1-x64
	  sudo ./install
	  sudo chmod +x /usr/jexus/*

	  sudo sed -i 's/root=\/ \/var\/www\/default/root=\/ \/wwwroot\/htdocs\/www/g' /usr/jexus/siteconf/default

	fi
	
	cd /tmp/downloads
	wget -c https://github.com/cjy37/linux-asp.net-installScript/blob/master/aspx-info.tar.gz?raw=true
	if [ -f aspx-info.tar.gz ]; then
	  tar xvf aspx-info.tar.gz
	  cd aspx-info
	  sudo cp -r -f index.aspx /wwwroot/htdocs/www/index.aspx
	  echo "站点根目录: /wwwroot/htdocs/www/"
	  echo "如果你要更新它. 请修改 Jexus 配置文件：(/usr/jexus/siteconf/*)"
	fi
	
	sudo /usr/jexus/jws start
	sudo /usr/jexus/jws -V
	echo "------------------------------------"
	echo "Jexus 安装目录: /usr/jexus/"
	echo "Jexus 配置文件：(/usr/jexus/jws.conf and /usr/jexus/siteconf/*)"
	  
	return $?
}

cd /tmp

read -n 1 -p "按任意键安装公共组件. 按 [Ctrl + C] 取消安装."
setupLibs
selectCmd
